<?php


namespace SidToken\Utils;


use SidToken\Utils\TronUtil;

class TrxRpc
{
    //public $baseUrl = "http://92.204.134.116:16888/rpc/account";
    public $tag = "TAG";


    private $tronKeys = [
        "8f117d35-67c0-42b1-984c-7ffab9bc4035",
        "4e5e0a85-f175-4499-bf7a-cd990256f9f0",
        "c84ffe48-69d1-465d-a479-51faf6691d34",
        "5bf4eff0-7fad-4724-8da4-4ed563477eca",
        "d6ae9f02-dc82-4d15-82a2-a2d7f40d3478",
        "42379691-f6ef-4676-afc4-0da3896f3935",
        "0d9edcbe-681f-4216-bdd0-d2bbdba29d0b",
        "3c856261-2ff4-4c7f-a649-1d223b30cee3",
        "64411ee1-d2c7-4a3d-87b8-e22636d733d9",
        "33d1cd40-ab6a-4ff1-ae16-3b50be64c922",
        "f9fb4123-661d-40a2-8fdf-1d429747e69c",
        "0f4e1538-2b10-42fc-bb9c-3d9a5710bd1d",
        "557a2357-6ab2-4b24-8a03-1c6294414ab9",
        "cb577d9e-2a31-4308-8908-e4a5b578381f",
        "30f28646-bd72-443a-ab2e-5029cd21e861",
        "4bf141eb-1565-455a-b354-dcc8a6d0eb8e",
        "63b2e8e3-b6d8-4079-9fb1-feb9170cc4f0",
        "bb54ce7a-9d7e-4d2a-82b8-e44aa0cf1b4b",
        "b2d159b5-c560-47c6-b35e-a6c4f684ca29",
        "b081e2df-ea04-4cac-9b69-6b1e59d2ef9f",
        "a08e9d32-3e47-4df5-9fb5-4ebe6a7fad7c",
        "830859c1-4d6a-4bb3-b8b6-0f1569d87d0f",
        "567335e0-de2c-45fc-b9ea-4f9e7afb8303",
        "e465bcf2-d7cc-4323-8c14-14e12543afdb",
        "d9930164-5462-4690-b1fb-846287a164ff",
        "dd2239e9-1e5e-4a8e-ae08-05b41a6a09a6",
        "c017c8d8-d71d-4878-a1cc-298f6f64d5e1",
        "38bab6d3-3ef4-430c-b5a9-028f0be62cab",
        "6f2ac6c8-a341-49f5-bf50-1afb92c7d161",
        "04682b3b-614e-4bca-9414-0dfbcf6216bc",
        "b5da03fa-cc03-4356-ae4f-b4fbe5753a97",
        "85b77421-18a7-4de6-a59b-cbf27dc8a908",
        "f73f6cac-9289-4820-8618-e72bb8d5e2b1",
        "df268564-239f-46f0-813d-6aefa68b431b",
        "6b4598a0-6752-4ac6-a7a9-a77e61d1aeeb",
        "617f5122-1dce-4778-8e48-12c3232568c2",
        "193764db-2e52-4f55-a0f9-4c24a84cb5cd",
        "37410879-dc74-4a5b-b191-d2e31e22df60",
        "f1007002-6d68-4e4b-9dec-4091fb89a5d5",
        "70502bcf-6a0e-4e05-b9f8-74c75f5f6d8f",
        "24f9f978-3950-4392-af7b-8c4db6b682cb",
        "1a21956b-b039-46bf-bb9d-25098cd75b67",
        "10dc345d-9c3f-4ecc-bb2f-05a07676ccdc",
        "a62809eb-cc24-45f5-bbd5-e605e2eb351a",
        "6e38e3a8-440b-4c19-92ca-6e61996ee849",
        "4a209acd-34ba-464a-94f8-3c409da6c674",
        "cb482679-d246-4b11-b92d-74da7e0738da",
        "3d7134a7-2a2a-4b6e-926e-52a2eae91016",
        "c2063f50-6147-40eb-ae9e-ac50e7ab25c8",
        "74d2f9bd-3e10-401c-b4f8-d496d71f59f3",
        "d90e3ed1-9087-48bc-8940-5c375a9923f2",
        "442ce0a7-974b-4c08-9251-91ee2de65d83",
        "f4ec0139-d8ed-4e7f-863a-1f015156cfa6",
        "c126075c-ebdf-4bd1-a872-d60f244c32ed",
        "2731c3ca-2384-420f-b158-e54c11a73500",
        "87497f5f-14f4-4a86-8e74-76aa86b9b505",
        "53d29c4e-3c46-4071-85c9-5914c66d9fef",
        "80d823fb-dc0b-4a90-bca2-f381fc5f9df1"
    ];

    public function getEthAddress()
    {
        $result = curlGet("http://eth-tools-demo.btcsoft.cn/index.php");
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * 生成地址
     * @param $username
     * @return mixed
     * @throws \Exception
     */
    public function getAddress($username)
    {
        $TronUtil = new TronUtil();
        $rs = $TronUtil->getAddress($username);
        if ($rs == null) {
            return $this->jerror("tron wallet api error");
        }
        $rs = json_decode($rs, true);
        if ($rs['status'] == 'SUCCESS') {
            $address = $rs['data'];
            $return = [
                'address' => $address
            ];
            return $this->jsuccess($return);
        } else {
            if ($rs['status'] == 500) {
                return $this->jerror($rs['message']);
            } else {
                return $this->jerror($rs['data']['msg']);
            }
        }
    }

    /*
     * common function
     */
    public function jsuccess($data = '', $msg = '')
    {
        return $this->json(1, $msg, $data);
    }

    public function jerror($msg = '', $data = '', $ret = 10000)
    {
        return $this->json($ret, $msg, $data);
    }

    public function json($ret = 0, $msg = '', $data = '')
    {
        header('Content-Type:application/json; charset=utf-8');
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Allow-Headers:*');
        header("Access-Control-Request-Headers:*");
        $return = array(
            'status' => $ret,
            'info' => $msg,
            'data' => $data
        );
        return $return;
    }
}